package com.hl.ruoyilkshop.service.impl;

import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hl.ruoyilkshop.mapper.GoodsSpecRelationMapper;
import com.hl.ruoyilkshop.domain.GoodsSpecRelation;
import com.hl.ruoyilkshop.service.IGoodsSpecRelationService;
import com.ruoyi.common.core.utils.DateUtils;

/**
 * 商品规格关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-08
 */
@Service
public class GoodsSpecRelationServiceImpl implements IGoodsSpecRelationService
{
    @Autowired
    private GoodsSpecRelationMapper goodsSpecRelationMapper;

    /**
     * 查询商品规格关联
     * 
     * @param id 商品规格关联主键
     * @return 商品规格关联
     */
    @Override
    public GoodsSpecRelation selectGoodsSpecRelationById(Long id)
    {
        return goodsSpecRelationMapper.selectGoodsSpecRelationById(id);
    }

    /**
     * 查询商品规格关联详情（包含规格值）
     * 
     * @param id 商品规格关联主键
     * @return 商品规格关联详情
     */
    @Override
    public GoodsSpecRelation selectGoodsSpecRelationDetail(Long id)
    {
        return goodsSpecRelationMapper.selectGoodsSpecRelationDetail(id);
    }

    /**
     * 查询商品规格关联详情（从SKU表获取商品对应的具体规格值）
     * 
     * @param id 商品规格关联主键
     * @return 商品规格关联详情
     */
    @Override
    public GoodsSpecRelation selectGoodsSpecRelationDetailWithSku(Long id)
    {
        return goodsSpecRelationMapper.selectGoodsSpecRelationDetailWithSku(id);
    }

    /**
     * 从SKU表获取商品对应的具体规格值
     * 
     * @param id 商品规格关联主键
     * @return 商品规格关联详情
     */
    @Override
    public GoodsSpecRelation selectSpecsFromSku(Long id)
    {
        return goodsSpecRelationMapper.selectSpecsFromSku(id);
    }

    /**
     * 查询商品规格关联列表
     * 
     * @param goodsSpecRelation 商品规格关联
     * @return 商品规格关联
     */
    @Override
    public List<GoodsSpecRelation> selectGoodsSpecRelationList(GoodsSpecRelation goodsSpecRelation)
    {
        // 先查询基础的商品规格关联信息（不包含规格值）
        List<GoodsSpecRelation> baseList = goodsSpecRelationMapper.selectGoodsSpecRelationList(goodsSpecRelation);
        
        // 为每个商品规格关联查询对应的规格值
        List<GoodsSpecRelation> resultList = new ArrayList<>();
        for (GoodsSpecRelation relation : baseList) {
            // 查询该商品规格关联的详细规格值
            GoodsSpecRelation detail = goodsSpecRelationMapper.selectGoodsSpecRelationDetail(relation.getId());
            if (detail != null) {
                resultList.add(detail);
            } else {
                resultList.add(relation);
            }
        }
        
        return resultList;
    }

    /**
     * 新增商品规格关联
     * 
     * @param goodsSpecRelation 商品规格关联
     * @return 结果
     */
    @Override
    public int insertGoodsSpecRelation(GoodsSpecRelation goodsSpecRelation)
    {
        goodsSpecRelation.setCreateTime(DateUtils.getNowDate());
        return goodsSpecRelationMapper.insertGoodsSpecRelation(goodsSpecRelation);
    }

    /**
     * 修改商品规格关联
     * 
     * @param goodsSpecRelation 商品规格关联
     * @return 结果
     */
    @Override
    public int updateGoodsSpecRelation(GoodsSpecRelation goodsSpecRelation)
    {
        return goodsSpecRelationMapper.updateGoodsSpecRelation(goodsSpecRelation);
    }

    /**
     * 批量删除商品规格关联
     * 
     * @param ids 需要删除的商品规格关联主键
     * @return 结果
     */
    @Override
    public int deleteGoodsSpecRelationByIds(Long[] ids)
    {
        return goodsSpecRelationMapper.deleteGoodsSpecRelationByIds(ids);
    }

    /**
     * 删除商品规格关联信息
     * 
     * @param id 商品规格关联主键
     * @return 结果
     */
    @Override
    public int deleteGoodsSpecRelationById(Long id)
    {
        return goodsSpecRelationMapper.deleteGoodsSpecRelationById(id);
    }
}
